%%%-------------------------------------------------------------------
%%% @doc  
%%% Copyright (C) 2007, Lucas Carlson
%%% File    : drproxy_client.erl
%%% Author  : Lucas Carlson (lucas@rufy.com)
%%% @end
%%%-------------------------------------------------------------------
-module(drproxy_client).

-behaviour(application).
%%--------------------------------------------------------------------
%% Include files
%%--------------------------------------------------------------------
-include("drproxy_client.hrl").

%%--------------------------------------------------------------------
%% External exports
%%--------------------------------------------------------------------
-export([
	 start/2, shutdown/0, stop/1,
	 config/1, servers/0, client_port/1, pid_file/1, resource_pid/1,
	 status/0
	 ]).

%%--------------------------------------------------------------------
%% Macros
%%--------------------------------------------------------------------

%%--------------------------------------------------------------------
%% Records
%%--------------------------------------------------------------------

%%====================================================================
%% External functions
%%====================================================================
%%--------------------------------------------------------------------
%% @doc The starting point for an erlang application.
%% @spec start(Type, StartArgs) -> {ok, Pid} | {ok, Pid, State} | {error, Reason}
%% @end
%%--------------------------------------------------------------------
start(Type, StartArgs) ->
  case dpc_sup:start_link(StartArgs) of
    {ok, Pid} -> 
      {ok, Pid};
    Error ->
      Error
  end.

%%--------------------------------------------------------------------
%% @doc Called to shudown the drproxy_client application.
%% @spec shutdown() -> ok 
%% @end
%%--------------------------------------------------------------------
shutdown() ->
    application:stop(drproxy_client).

config(Type) ->
	case config_server:config(Type) of
	  not_found ->
	    error_logger:error_msg("default value not found for key ~w~n", [Type]),
			exit({default_value, not_found});
		Value ->
		  Value
	end.

servers() ->
	config(servers).

client_port(N) ->
	config(client_first_port)+N-1.

pid_file(N) ->
	lists:flatten(io_lib:format(config(pid_file), [client_port(N)])).

resource_pid(N) ->
	CatCmd = lists:flatten(io_lib:format("cat ~s", [pid_file(N)])),
	Cat = os:cmd(CatCmd),
	list_to_integer(string:substr(Cat, 0, length(Cat)-1)).

status() ->
	dpc_server:status(),
	resource_server:status(),
	process_server:status().

%%====================================================================
%% Internal functions
%%====================================================================

%%--------------------------------------------------------------------
%% Called upon the termintion of an application.
%%--------------------------------------------------------------------
stop(State) ->
    ok.